﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.IO.Compression;

namespace IPC.Communication.BasicFramework
{
    public class SoftZipped
    {
        public static byte[] CompressBytes(byte[] bytes)
        {
            bool flag = bytes == null;
            if (flag)
            {
                throw new ArgumentNullException("bytes");
            }
            byte[] result;
            using (MemoryStream memoryStream = new MemoryStream())
            {
                using (GZipStream gZipStream = new GZipStream(memoryStream, CompressionMode.Compress))
                {
                    gZipStream.Write(bytes, 0, bytes.Length);
                }
                result = memoryStream.ToArray();
            }
            return result;
        }

        public static byte[] Decompress(byte[] bytes)
        {
            bool flag = bytes == null;
            if (flag)
            {
                throw new ArgumentNullException("bytes");
            }
            byte[] result;
            using (MemoryStream memoryStream = new MemoryStream(bytes))
            {
                using (GZipStream gZipStream = new GZipStream(memoryStream, CompressionMode.Decompress))
                {
                    using (MemoryStream memoryStream2 = new MemoryStream())
                    {
                        int num = 1024;
                        byte[] buffer = new byte[num];
                        int count;
                        while ((count = gZipStream.Read(buffer, 0, num)) > 0)
                        {
                            memoryStream2.Write(buffer, 0, count);
                        }
                        result = memoryStream2.ToArray();
                    }
                }
            }
            return result;
        }
    }
}
